package com.bupt.controller.common;


import com.bupt.annotation.AccessLimit;
import com.bupt.constants.ResponseResult;
import com.bupt.dto.sys.UserDto;
import com.bupt.service.common.NodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = {"项目节点的操作"})
public class NodeController {

    @Resource
    private NodeService nodeservice;

    @ApiOperation("增加节点")
    @AccessLimit(interfaceName = "增加节点")
    @PostMapping(value = "/projects/{projectId}/nodes")
    public Object addNode(@ApiIgnore UserDto userDto,
                          @PathVariable("projectId")String id,
                          @RequestParam("code") String code,
                          @RequestParam("name")String nodeName,
                          @RequestParam("longitude") Double longitude,
                          @RequestParam("latitude") Double latitude,
                          @RequestParam(value = "year")int year,
                          @RequestParam(value = "completedYear") int completedYear,
                          @RequestParam(value = "serviceLength")int serviceLength,
                          @RequestParam(value = "capacity") int capacity
    ){
        try {
            Object o = nodeservice.addNode(userDto,id, nodeName, year, completedYear, serviceLength, capacity, longitude, latitude, code);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());

        }
    }

    @ApiOperation("删除节点")
    @AccessLimit(interfaceName = "删除节点")
    @DeleteMapping(value = "/projects/{projectId}/nodes/{id}")
    public Object deleteNode(@ApiIgnore UserDto userDto,@PathVariable("projectId")String projectId,@PathVariable("id")String nodeId){
        try {
            Object o = nodeservice.deleteNode(userDto,projectId, nodeId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return  ResponseResult.createFail(e.getMessage());
        }
    }


    @ApiOperation("更新节点")
    @AccessLimit(interfaceName = "更新节点")
    @PutMapping(value = "/projects/{projectId}/nodes/{id}")
    public Object updateNode(
            @ApiIgnore UserDto userDto,
            @PathVariable("projectId") String projectId,
            @PathVariable("id") String nodeId,
            @RequestParam("name") String nodeName,
            @RequestParam(value = "year") int year,
            @RequestParam(value = "completedYear",required = false) int completedYear,
            @RequestParam(value = "serviceLength",required = false) int serviceLength,
            @RequestParam(value = "capacity") int capacity,
            @RequestParam("longitude") Double longitude,
            @RequestParam("latitude") Double latitude,
            @RequestParam("code") String code
    ){
        try {
            Object o = nodeservice.updateNode(userDto, projectId, nodeId, nodeName, year, completedYear, serviceLength,
                    longitude, latitude, code, capacity);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("获取节点列表")
    @AccessLimit(interfaceName = "获取节点列表")
    @GetMapping(value ="/projects/{projectId}/nodes")
    public Object getProjectNodeList(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId){
            try {
                Object o = nodeservice.getProjectNodeList(userDto, projectId);
                return ResponseResult.createOK(o);
            }catch (Exception e){
                return ResponseResult.createFail(e.getMessage());
            }
    }



}
